{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "import numpy\n",
    "from six.moves import xrange  # pylint: disable=redefined-builtin\n",
    "import tensorflow as tf\n",
    "import input_data\n",
    "import c3d_model\n",
    "import math\n",
    "import numpy as np\n",
    "\n",
    "MOVING_AVERAGE_DECAY = 0.9999\n",
    "\n",
    "def placeholder_inputs(batch_size):\n",
    "    \"\"\"Generate placeholder variables to represent the input tensors.\n",
    "      These placeholders are used as inputs by the rest of the model building\n",
    "      code and will be fed from the downloaded data in the .run() loop, below.\n",
    "      Args:\n",
    "        batch_size: The batch size will be baked into both placeholders.\n",
    "      Returns:\n",
    "      \n",
    "        images_placeholder: Images placeholder.\n",
    "        labels_placeholder: Labels placeholder.\n",
    "    \"\"\"\n",
    "    # Note that the shapes of the placeholders match the shapes of the full\n",
    "    # image and label tensors, except the first dimension is now batch_size\n",
    "    # rather than the full size of the train or test data sets.\n",
    "    images_placeholder = tf.placeholder(tf.float32, shape=(batch_size,\n",
    "                                                           c3d_model.NUM_FRAMES_PER_CLIP,\n",
    "                                                           c3d_model.CROP_SIZE,\n",
    "                                                           c3d_model.CROP_SIZE,\n",
    "                                                           c3d_model.CHANNELS))\n",
    "    labels_placeholder = tf.placeholder(tf.int64, shape=(batch_size))\n",
    "    return images_placeholder, labels_placeholder\n",
    "\n",
    "def average_gradients(tower_grads):\n",
    "    average_grads = []\n",
    "    for grad_and_vars in zip(*tower_grads):\n",
    "        grads = []\n",
    "        for g, _ in grad_and_vars:\n",
    "            expanded_g = tf.expand_dims(g, 0)\n",
    "            grads.append(expanded_g)\n",
    "        grad = tf.concat(grads,0)\n",
    "        grad = tf.reduce_mean(grad, 0)\n",
    "        v = grad_and_vars[0][1]\n",
    "        grad_and_var = (grad, v)\n",
    "        average_grads.append(grad_and_var)\n",
    "    return average_grads\n",
    "\n",
    "def tower_loss(name_scope, logit, labels):\n",
    "    cross_entropy_mean = tf.reduce_mean(\n",
    "                  tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logit, labels=labels)\n",
    "                  )\n",
    "    tf.summary.scalar(name_scope + 'cross_entropy',cross_entropy_mean)\n",
    "    #tf.scalar_summary(name_scope + 'cross entropy',cross_entropy_mean)\n",
    "    weight_decay_loss = tf.add_n(tf.get_collection('losses', name_scope))\n",
    "    #tf.scalar_summary(name_scope + 'weight decay loss', weight_decay_loss)\n",
    "    tf.summary.scalar(name_scope + 'weight_decay_loss', weight_decay_loss)\n",
    "    tf.add_to_collection('losses', cross_entropy_mean)\n",
    "    losses = tf.get_collection('losses', name_scope)\n",
    "\n",
    "  # Calculate the total loss for the current tower.\n",
    "    total_loss = tf.add_n(losses, name='total_loss')\n",
    "    tf.summary.scalar(name_scope + 'total_loss', total_loss)\n",
    "\n",
    "  # Compute the moving average of all individual losses and the total loss.\n",
    "    loss_averages = tf.train.ExponentialMovingAverage(0.99)###################################################\n",
    "    loss_averages_op = loss_averages.apply(losses + [total_loss])\n",
    "    with tf.control_dependencies([loss_averages_op]):\n",
    "        total_loss = tf.identity(total_loss)\n",
    "\n",
    "        return total_loss\n",
    "\n",
    "def tower_acc(logit, labels):\n",
    "    correct_pred = tf.equal(tf.argmax(logit, 1), labels)\n",
    "    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))\n",
    "    return accuracy\n",
    "\n",
    "def _variable_on_cpu(name, shape, initializer):\n",
    "    with tf.device('/cpu:0'):\n",
    "        var = tf.get_variable(name=name, shape=shape, initializer=initializer)\n",
    "    return var\n",
    "\n",
    "def _variable_with_weight_decay(name, shape, wd):\n",
    "    var = _variable_on_cpu(name=name, shape=shape, initializer=tf.contrib.layers.xavier_initializer())\n",
    "    if wd is not None:\n",
    "        weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss')\n",
    "        tf.add_to_collection('losses', weight_decay)\n",
    "    return var\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
